package com.example.jhweather.Base;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.jhweather.R;
import com.example.jhweather.Utils.StatusBar.ResUtils;
import com.example.jhweather.Utils.StatusBar.StatusBarUtils;

public class BaseActivity extends AppCompatActivity {
    private Activity activity = new Activity();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //沉浸式状态栏设置
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        //设置状态栏夜色为黑色
        StatusBarUtils.setStatusBarLightMode(getWindow(), statusBarColor());

        //设置状态颜色为白色
        //StatusBarUtils.setStatusBarDarkMode(getWindow(), statusBarColor());

        /**
         * 这两句话也可分别设置状态栏字体颜色为黑色和白色
         */
        //window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        //window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

    }


    private void setStatusBarColor(@ColorRes int resId) {
        StatusBarUtils.setStatusBarColor(this, resId);

        // 自动根据状态栏颜色的深浅来设置状态栏颜色
        int color = ResUtils.getColor(this, resId);
        float a = color >>> 24;
        float r = (color & 0xff0000) >> 16;
        float g = (color & 0xff00) >> 8;
        float b = color & 0xff;

        // 状态栏颜色较浅，使用黑色的状态栏文字
        boolean lightColor = r * 0.299 + g * 0.578 + b * 0.114 >= 192;
        setStatusBarLightMode(lightColor);
    }

    /**
     * 设置状态栏模式。
     *
     * @param lightMode true-亮色模式，状态栏文字显示为黑色 false-暗色模式，状态栏文字颜色为白色
     */
    protected void setStatusBarLightMode(boolean lightMode) {
        if (lightMode) {
            StatusBarUtils.setStatusBarLightMode(getWindow(), statusBarColor());
        } else {
            StatusBarUtils.setStatusBarDarkMode(getWindow(), statusBarColor());
        }
    }

    /**
     * 状态栏颜色
     *
     * @return 状态栏颜色
     */
    @ColorRes
    protected int statusBarColor() {
        return R.color.design_default_color_primary_dark;
    }

}
